package com.renyq.algorithm.graph

import com.renyq.algorithm.graph.applicate.GraphBfs.bfs
import kotlin.jvm.JvmStatic
import com.renyq.algorithm.graph.structure.Edge
import com.renyq.algorithm.graph.structure.Graph
import com.renyq.algorithm.graph.structure.Node

object GraphGenerator {
    /**
     * [3,1,4]
     * [5,4,3]
     * [2,2,1]
     * @param matrix
     * @return
     */
    fun createGraph(matrix: Array<IntArray>): Graph {
        val graph = Graph()
        for (integers in matrix) {
            val weight = integers[0]
            val from = integers[1]
            val to = integers[2]
            if (!graph.nodes.containsKey(from)) {
                graph.nodes[from] = Node(from)
            }
            if (!graph.nodes.containsKey(to)) {
                graph.nodes[to] = Node(to)
            }
            val fromNode = graph.nodes[from]
            val toNode = graph.nodes[to]
            fromNode!!.out++
            fromNode.nexts.add(toNode!!)
            toNode.`in`++
            val edge = Edge(
                weight, fromNode, toNode
            )
            fromNode.edges.add(edge)
            graph.edges.add(edge)
        }
        return graph
    }

    @JvmStatic
    fun main(args: Array<String>) {
        val matrix = arrayOf(intArrayOf(3, 1, 4), intArrayOf(5, 4, 3), intArrayOf(2, 2, 1))
        val graph = createGraph(matrix)
        bfs(graph.nodes[2])
    }
}